package app.netant.book.xs37;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class Xs37Detail extends NetAntBase {
	
	private Xs37Entity entity;

	public Xs37Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(Xs37Entity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select("#maininfo .coverecom .tabstit a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}	
	}
	
	public void analyseTitle(){
		Elements activePart = document.select("#maininfo #bookinfo .bookright .d_title h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".txt");
		}	
	}
	
	public void analyseWriter(){
		Elements activePart = document.select("#maininfo #bookinfo .bookright .d_title p.p_author a");
		if(activePart.size()>0){
			String writer = activePart.get(0).text();
			System.out.println("【Writer】:"+writer);
			this.entity.setNovelWriter(writer);
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select("#maininfo #bookinfo .bookright #bookintro #showinfo div.d2");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select("#maininfo .coverecom #bookinfo #button_all ul li.li3 a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			System.out.println("【Href】:"+href);
			this.entity.setDownPath(href);
		}
		
		if(this.entity.getDownPath() == null || "".equals(this.entity.getDownPath())){
			Elements newPart = document.select("#maininfo #button_all ul li.li3 a");
			if(newPart.size()>0){
				String href = newPart.get(0).attr("href");
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
			}
		}
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseWriter();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		Xs37Detail detail = new Xs37Detail("https://www.37xs.com/html/18/18216/");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseWriter();
		detail.analyseMemo();
		detail.analyseHref();
		
	}
	
}
